<template>
	<view class="container">

		<view class="user-section">
			<image class="bg" src="/static/user-bg.png"></image>
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" src="../../static/logo.png"></image>
				</view>
				<view class="info-box">
					<view class="username text-white">{{hasLogin ? userInfo.name : '游客'}}</view>
					<view class="username text-id text-white" @click="navTo('')">{{hasLogin ? 'ID: '+userInfo.userId : '点此登录'}}</view>
				</view>
			</view>
			<view class="vip-card-box">
				<image class="card-bg " src="/static/vip-card-bg.jpg" mode="widthFix"></image>
				<view class="b-btn">
					{{userInfo.isMember == '1' ? "已开启" : "立即开启"}}
				</view>
				<view class="tit">
					<text class="yticon icon-iLinkapp-"></text>
					尊贵会员
				</view>
				<text class="e-m">消费即可成为会员</text>
				<text class="e-b">寻找伙伴 获得赏金</text>
			</view>
		</view>

		<view class="cover-container" :style="[{
				transform: coverTransform,
				transition: coverTransition
			}]"
		 @touchstart="coverTouchstart" @touchmove="coverTouchmove" @touchend="coverTouchend">
			<image class="arc" src="/static/arc.png"></image>

			<view class="tj-sction">
				<view class="tj-item" @click="navTo('/pages/friends/friends')">
					<text class="num">{{friends}}</text>
					<text>伙伴</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/coupon/coupon')">
					<text class="num">{{coupon}}</text>
					<text>折扣券</text>
				</view>
				<view class="tj-item" @click="navTo('/pages/integral/integral')">
					<text class="num">{{userInfo.integral}}</text>
					<text>积分</text>
				</view>
			</view>
			<!-- 订单 -->
			<view class="order-section">
				<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-hot"></text>
					<text>热卖订单</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-shouye"></text>
					<text>折扣订单</text>
					<text class="pop" v-if="discountOrder>0">{{discountOrder}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-yishouhuo"></text>
					<text>已发货</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=3')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-daifukuan"></text>
					<text>挂售中</text>
					<text class="pop" v-if="saleingOrder>0">{{saleingOrder}}</text>
				</view>
				<view class="order-item" @click="navTo('/pages/order/order?state=4')" hover-class="common-hover" :hover-stay-time="50">
					<text class="yticon icon-shouhoutuikuan"></text>
					<text>已售出</text>
				</view>
			</view>
			<!-- 浏览历史 -->
			<view class="history-section icon">
				<!-- <view class="sec-header" :class="{'padding-top':looked.length > 0}" v-if="looked.length > 0">
					<text class="yticon icon-lishijilu"></text>
					<text>浏览历史</text>
				</view>
				<scroll-view scroll-x class="h-list" v-if="looked.length > 0">
					<image v-for="item in looked" :key="item.id" @click="navTo('/pages/product/product?id='+item[0]+'&type=1')" :src="host + item[2]"
					 mode="aspectFill"></image>
				</scroll-view> -->

				<list-cell icon="icon-dizhi" iconColor="#5fcda2" title="地址管理" @eventClick="navTo('/pages/address/address')"></list-cell>
				<list-cell icon="icon-iconfontweixin" iconColor="#ee883b" title="银行卡管理" @eventClick="navTo('/pages/card/card')"
				 tips="积分可提现"></list-cell>
				<list-cell icon="icon-tuandui" iconColor="#e54d42" title="代理申请" @eventClick="navTo('/pages/agent/agent?name='+userInfo.name+'&phone='+userInfo.mobileNumber)"></list-cell>
				<list-cell icon="icon-fenlei" iconColor="#8dc63f" title="管理规则" @eventClick="navToNofilter('/pages/rule/rule?state=0')"></list-cell>
				<list-cell icon="icon-tuijian" iconColor="#1cbbb4" title="会员协议" @eventClick="navToNofilter('/pages/rule/rule?state=1')"></list-cell>
				<list-cell icon="icon-bangzhu" iconColor="#9c26b0" title="联系客服" @eventClick="navToNofilter('/pages/service/service')"></list-cell>
				<!--<list-cell icon="icon-shoucang_xuanzhongzhuangtai" iconColor="#54b4ef" title="我的收藏" @eventClick="navTo('/pages/collection/collection')"></list-cell>-->
				<list-cell icon="icon-shezhi1" iconColor="#e07472" title="设置" border="" @eventClick="navTo('/pages/set/set')"></list-cell>
			</view>
			<!--
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-red margin-tb-sm lg">退出登录</button>
			</view>-->
		</view>
	</view>
</template>
<script>
	import listCell from '@/components/mix-list-cell';
	import {
		mapState
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			listCell
		},
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				coupon: 0,
				friends: 0,
				hasLogin: false,
				userInfo: {
					integral: 0
				},
				looked: [],
				host: '',
				saleingOrder:0,
				discountOrder:0
			}
		},
		onShow() {
			this.host = this.$host;
			this.init();
		},
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		methods: {
			/* 初始化 */
			init() {
				let token = uni.getStorageSync("TOKEN");
				var that = this;
				if (token) {
					this.hasLogin = true;
					that.$network.request("UserAction/init", {}, function(res) {
						that.userInfo = res.data.user;
						that.coupon = res.data.coupon;
						that.friends = res.data.friends;
					}, "", function(res) {
						console.error(JSON.stringify(res))
						uni.showToast({
							icon: 'none',
							title: '加载失败',
							duration: 1200
						})
					});
					// that.$network.request("LookRecordAction/getMyLook", {}, function(res) {
					// 	that.looked = res.data.list;
					// }, "", function(res) {
					// 	console.error(JSON.stringify(res))
					// 	uni.showToast({
					// 		icon: 'none',
					// 		title: '加载失败',
					// 		duration: 1200
					// 	})
					// });
					that.$network.request("OrdersAction/getOrderCountByUser", {}, function(res) {
						that.saleingOrder = res.data.saleingOrder
						that.discountOrder = res.data.discountOrder
					}, "", function(res) {
						
					});
				} else {
					this.hasLogin = false;
					this.moving = false;
					this.coupon = 0;
					this.friends = 0;
					this.userInfo = {
						integral: 0
					};
					this.looked = [];
				}
			},
			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				if (url) {
					uni.navigateTo({
						url
					})
				}
			},
			/**
			 * 统一跳转接口,不拦截拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navToNofilter(url) {
				if (url) {
					uni.navigateTo({
						url
					})
				}
			},
			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}

				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			}
		}
	}
</script>
<style lang='scss'>
	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	.user-section {
		height: 420upx;
		padding: 0 30upx 0;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			font-size: $font-lg + 6upx;
			margin-left: 20upx;
		}

		.text-id {
			font-size: 24upx;
		}

	}

	.vip-card-box {
		display: flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0, 0, 0, .7), rgba(0, 0, 0, .8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;

		.card-bg {
			position: absolute;
			top: 20upx;
			right: 0;
			width: 100%;
			height: 260upx;
			z-index: 0;
			border-radius: 10rpx;
		}

		.b-btn {
			position: absolute;
			right: 20upx;
			top: 40upx;
			width: 132upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 22upx;
			color: #ffaa00;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
		}

		.tit {
			margin-top: 10px;
			z-index: 1;
			font-size: $font-base+2upx;
			color: #f7d680;
			margin-bottom: 28upx;

			.yticon {
				color: #f6e5a3;
				margin-right: 16upx;
			}
		}

		.e-b {
			z-index: 1;
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}

	.cover-container {
		background: $page-color-base;
		margin-top: -150upx;
		padding: 0 30upx;
		position: relative;
		background: #f5f5f5;
		padding-bottom: 20upx;
		z-index: 10;

		.arc {
			z-index: 10;
			position: absolute;
			left: 0;
			top: -14upx;
			width: 100%;
			height: 36upx;
		}
	}

	.tj-sction {
		@extend %section;

		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: #75787d;
		}

		.num {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}

	.order-section {
		@extend %section;
		padding: 28upx 0;
		margin-top: 20upx;

		.order-item {
			@extend %flex-center;
			width: 120upx;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;
			position: relative;
		}

		.yticon {
			font-size: 48upx;
			margin-bottom: 18upx;
			color: #fa436a;
		}

		.icon-shouhoutuikuan {
			font-size: 44upx;
		}
	}

	.history-section {
		margin-top: 20upx;
		background: #fff;
		border-radius: 10upx;

		.sec-header {
			display: flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;

			.yticon {
				font-size: 44upx;
				color: #5eba8f;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}

		.h-list {
			white-space: nowrap;
			padding: 30upx 30upx 0;

			image {
				display: inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}
	}
	.pop{
		position: absolute;
		top: 0;
		right: 10rpx;
		width: 30rpx;
		height: 30rpx;
		line-height: 30rpx;
		background-color: red;
		border-radius: 50%;
		font-size: 16rpx;
		text-align: center;
		color: #ffffff;
	}
</style>
